package com.sskjdata.cg.controller;

import com.sskjdata.cg.dto.ResponseCodeEnum;
import com.sskjdata.cg.exception.UnloginedException;
import com.sskjdata.cg.jwt.JwtUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Descirption controller 基类
 * @Author Jason
 * @Date 2018/6/14/
 */
public class BaseController {

    public static  final  String SUCCESS ="success";
    public static  final  String FAILURE ="false";


    /**
     * 获取当前登陆的用户
     * @return
     */
    public JwtUser getJwtUser() throws Exception{
        if(SecurityContextHolder.getContext().getAuthentication() == null){//未登录
            throw new UnloginedException(ResponseCodeEnum.AUTH_LOGIN_IS_REQUIRED);
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return  (JwtUser) authentication.getPrincipal();
    }

}
